package com.baseframe.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baseframe.auth.utils.AuthUtil;
import com.baseframe.blog.entity.SnsBlogAdmire;
import com.baseframe.blog.entity.request.SnsBlogAdmireRequest;
import com.baseframe.blog.service.SnsBlogAdmireService;
import com.baseframe.tool.container.Kv;
import com.baseframe.tool.result.R;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/front/blog/admire")
@AllArgsConstructor
public class FrontSnsBlogAdmireController {

    private SnsBlogAdmireService snsBlogAdmireService;


    @PostMapping("/add")
    public R<Object> addAdmire(@RequestBody SnsBlogAdmireRequest request){

        if(request.getBlogId()==null){
            return R.fail("点赞失败:缺少必要请求参数~");
        }

        SnsBlogAdmire snsBlogAdmire = new SnsBlogAdmire();
        BeanUtils.copyProperties(request,snsBlogAdmire);
        snsBlogAdmire.setUserId(AuthUtil.getUserId());

        return snsBlogAdmireService.save(snsBlogAdmire)?R.success():R.fail();

    }

    /**
     * 点赞数据
     * @param request
     * @return
     */
    @GetMapping("/count")
    public R<Object> getAdmireList(SnsBlogAdmireRequest request){
        if (request.getBlogId()==null){
            return R.fail("缺少必要请求参数~");
        }

        long count = snsBlogAdmireService.count(new LambdaQueryWrapper<SnsBlogAdmire>().eq(SnsBlogAdmire::getBlogId, request.getBlogId()));
        Kv kv = Kv.create();
        kv.put("admireCount",count);
        return R.data(kv);
    }
}
